package cn.nkjobsearch.html;

import java.io.BufferedReader;
import java.io.FileReader;

import cn.nkjobsearch.config.Config;
import cn.nkjobsearch.config.ConfigZhilian;
import cn.nkjobsearch.publicClass.File;

public class ComZhilianController {
	public ComZhilianController() {

	}

	public void startGetID() {
		readZhilian_PC(); 
		ComZhilianSearchList czpsl = new ComZhilianSearchList();
		czpsl.start();
		while (true) {
			if (!ConfigZhilian.ZHILIAN_SEARCHLIST_RUNNING) {
				writeZhilian_PC();
				break;
			}
			writeZhilian_PC();
			ConfigZhilian.ZHILIAN_SEARCHLIST_TIMER += 2;
			if (ConfigZhilian.ZHILIAN_SEARCHLIST_TIMER >= ConfigZhilian.ZHILIAN_SEARCHLIST_DELAY_SECONDS) {
				czpsl.close();
				czpsl.stop();
				writeZhilian_PC();
				ConfigZhilian.ZHILIAN_SEARCHLIST_TIMER = 0;
				System.out.println("Interrupt Zhilian Search list");
				czpsl = new ComZhilianSearchList();
				czpsl.start();
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void startGetHTML() {
		ComZhilianContent zlc = new ComZhilianContent();
		zlc.start();
		while (true) {
			/**
			 * 程序结束
			 */
			if(!ConfigZhilian.ZHILIAN_CONTENT_RUNNING) {
				break;
			}
			ConfigZhilian.ZHILIAN_CONTENT_TIMER++;
			if(ConfigZhilian.ZHILIAN_CONTENT_TIMER >= ConfigZhilian.ZHILIAN_CONTENT_DELAY_SECONDS) {
				zlc.close();
				zlc.stop();
				ConfigZhilian.ZHILIAN_CONTENT_TIMER = 0;
				System.out.println("Interrupt: GET Content Of Zhilian.");
				zlc = new ComZhilianContent();
				zlc.start();
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void writeZhilian_PC() {
		String confStr = ConfigZhilian.ZHILIAN_SEARCHLIST_P + "\r\n"
				+ ConfigZhilian.ZHILIAN_SEARCHLIST_C + "\r\n";
		File.writeConf(ConfigZhilian.ZHILIAN_SEARCHLIST_PIC_CONF_PATH, confStr);
	}

	private void readZhilian_PC() {
		try {
			BufferedReader bufferedReader = null;
			bufferedReader = new BufferedReader(new FileReader(
					ConfigZhilian.ZHILIAN_SEARCHLIST_PIC_CONF_PATH));
			ConfigZhilian.ZHILIAN_SEARCHLIST_P = Integer.parseInt(bufferedReader
					.readLine().toString());
			ConfigZhilian.ZHILIAN_SEARCHLIST_C = Integer.parseInt(bufferedReader
					.readLine().toString());
			bufferedReader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
